Promise.prototype.myFinally = function (callback) {
  return this.then((value) => {
    return Promise.resolve(callback()).then(() => value)
  }, (reason) => {
    return Promise.resolve(callback()).then(() => { throw reason })
  })
}
Promise.prototype.myCatch = function (callback) {
  return this.then(undefined, callback)
}
Promise.prototype.myAll = function (array) {
  let result = []
  let len = array.length
  let count = 0
  return new Promise((resolve, reject) => {
    function addData (value, index) {
      count++
      result[index] = value
      if (count === len) {
        resolve(result)
      }
    }
    array.forEach((item, index) => {
      if (item instanceof Promise) {
        item.then((value) => {
          addData(value, index)
        }, (reason) => {
          reject(reason)
        })
      } else {
        addData(item, index)
      }
    })
  })
}

Promise.prototype.myResolve = function (item) {
  if (item instanceof Promise) {
    return item
  } else {
    return new Promise((resolve) => resolve(item))
  }
}
Promise.prototype.myRace = function (array) {
  return new Promise((resolve, reject) => {
    array.forEach((item) => {
      Promise.resolve(item).then((value) => {
        resolve(value)
      }, (reason) => {
        reject(reason)
      })
    })
  })
}

Promise.prototype.allSettled = function (array) {
  let result = []
  return new Promise((resolve, rejetc) => {
    array.forEach((item, index) => {
      Promise.resolve(item).then((value) => {
        result[index] = {
          status: 'fulfilled',
          value: value
        }
        if (result.length === array.length && result.some((item) => item !== undefined)) {
          resolve(result)
        }
      }, (reason) => {

      })
    })
  })
}
Promise.prototype.any = function (array) {
  let index = 0
  return new Promise((resolve, reject) => {
    if (array.length === 0) return reject(new Error('作物'))
    array.forEach((p, i) => {
      Promise.resolve(p).then((value) => {
        resolve(value)
      }, (reason) => {
        index++
        if (index === array.length) {
          reject(new AggregateError('错误'))
        }
      })
    })
  })
}